/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dataStructure;

/**
 *
 * @author Mohamed.Azouz
 */
public class LinkedList {

    Node root;

    void add(String v) {
        if (root == null) {
            root = new Node(v);
            return;
        }
        root.addNode(new Node(v));
    }

    void showLinkedList() {
        root.show();
    }

    boolean searchLinkedList(String value) {
        return root.search(value);
    }

    int getNodeIndex(String value) {
        return root.getIndex(value, 0);
    }

    public static void main(String[] String) {
        LinkedList linkedList = new LinkedList();
        linkedList.add("MOhamed");
        linkedList.add("Ahmed");
        linkedList.add("Ali");
        linkedList.add("Azouz");
        linkedList.add("FOla");
        System.out.println(linkedList.searchLinkedList("Ali"));
        System.out.println(linkedList.getNodeIndex("aa"));
    }
}

class Node {

    String value;
    Node next = null;

    public Node(String value) {
        this.value = value;
    }

    void addNode(Node newNode) {
        if (this.next == null) {
            this.next = newNode;
            return;
        }
        this.next.addNode(newNode);
    }

    public void show() {
        System.out.println(this.value);
        if (this.next == null) {
            return;
        }
        this.next.show();
    }

    public boolean search(String value) {
        if (this.value.equals(value)) {
            return true;
        }
        if (this.next == null) {
            return false;
        }
        return this.next.search(value);
    }

    public int getIndex(String value, int index) {
        if (this.value.equals(value)) {
            return index;
        }
        if (this.next == null) {
            return -1;
        }
        return this.next.getIndex(value, index + 1);
    }
}